import os
from time import sleep

import allure
import pytest

import page
from page.page_jiamingceshi import PageJiamingceshi
from page.page_login import PageLogin
from page.page_wsy_study import PageStudy
from tool.get_driver import GetDriver
from tool.get_log import GetLogger
from tool.read_yaml import read_yaml

log = GetLogger.get_log()
module_name = str(os.path.basename(__file__)).split('.')[0]


@allure.epic("五十音页面")
@allure.feature("五十音学习功能测试用例")
class TestWsyAd:
    @classmethod
    def setup_class(cls):
        cls.driver = GetDriver.get_app_driver()
        cls.login = PageLogin(cls.driver)
        cls.wsy = PageJiamingceshi(cls.driver)
        cls.study = PageStudy(cls.driver)
        try:
            cls.login.base_click_element(page.approve)
        except:
            pass
        try:
            cls.login.base_click_element(page.skip_user_desc)
        except:
            pass
        cls.login.pagelogin('11100009001', '123456')
        cls.login.base_click_element(page.wsy_tab)

    @classmethod
    def teardown_class(cls):
        GetDriver.quit_app_driver()

    @allure.story("假名学习字母测试用例")
    @pytest.mark.parametrize("letter,roma_letter", read_yaml("wsy_study_letter.yaml"))
    def test01_jiaming_study(self, letter, roma_letter):
        self.wsy.pagejiamingceshi_click_start_study()
        self.study.pagestudy_click_study_1()
        try:
            self.study.pagestudy_click_study_cover_close()
        except:
            pass
        self.study.pagestudy_click_letter(letter)
        try:
            assert self.study.pagestudy_get_current_page_letter() == roma_letter
            log.info('断言成功')
            self.study.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.study.base_get_screenshot_as_file(module_name, '跟老师学弹窗失败')
            log.error('断言失败')
            raise
        finally:
            self.wsy.pagejiamingceshi_return_to_wsypage_keycode()

    @allure.title("人工复验")
    @allure.story("假名学习具体功能测试用例——人工复验(视频)")
    @pytest.mark.parametrize("record_screen", [module_name], indirect=True)
    def test02_jiaming_start_practice(self,record_screen):
        try:
            self.wsy.pagejiamingceshi_click_start_study()
            self.study.pagestudy_click_study_1()
            try:
                self.study.pagestudy_click_study_cover_close()
            except:
                pass
            with allure.step("视频关注点如下"):
                pass
            with allure.step("1.进入字母学习页面"):
                pass
            self.study.pagestudy_click_bottom_btn()
            sleep(3)
            with allure.step("2.点击底部返回按钮，返回字母选择页面"):
                pass
            self.study.pagestudy_click_bottom_back_btn()
            sleep(3)
            with allure.step("3.再次进入字母学习页面，查看笔画是否自动播放"):
                pass
            self.study.pagestudy_click_bottom_btn()
            sleep(3)
            with allure.step("4.点击播放按钮，查看笔画是否再次播放"):
                pass
            self.study.pagestudy_click_play_write_btn()
            sleep(3)
            with allure.step("5.点击书写按钮，查看是否能够书写"):
                pass
            self.study.pagestudy_click_write_btn()
            self.study.pagestudy_write_something()
            sleep(3)
            with allure.step("6.点击擦除按钮，查看是否能够擦除所写"):
                pass
            self.study.pagestudy_click_write_clear_btn()
            sleep(3)
            with allure.step("7.点击继续练习按钮，查看是否能够跳到发音页面"):
                pass
            self.study.pagestudy_click_bottom_btn()
            sleep(3)
            with allure.step("8.点击下一个假名按钮，查看是否能够跳到下一个假名"):
                pass
            self.study.pagestudy_click_bottom_btn()
            self.study.pagestudy_click_bottom_btn()
            sleep(3)
            assert True
        except:
            pass
        finally:
            self.wsy.pagejiamingceshi_return_to_wsypage_keycode()

    @allure.title("人工复验")
    @allure.story("学习视频播放功能——人工复验(视频)")
    @pytest.mark.parametrize("record_screen", [module_name], indirect=True)
    def test03_study_video(self, record_screen):
        try:
            self.wsy.pagejiamingceshi_click_start_study()
            self.study.pagestudy_click_study_video()
            self.study.pagestudy_click_study_video_play()
            with allure.step("1.查看视频是否播放(画面变化在第35秒左右)"):
                pass
            sleep(40)
            self.study.pagestudy_click_video_full_screen()
            with allure.step("2.查看视频是否全屏播放"):
                pass
            sleep(5)
            self.study.pagestudy_click_video_play_and_pause()
            with allure.step("3.查看视频是否暂停播放"):
                pass
            sleep(5)
            self.study.pagestudy_click_video_play_and_pause()
            with allure.step("4.查看视频是否可以再次播放"):
                pass
            sleep(5)
            self.study.pagestudy_click_video_full_screen()
            with allure.step("5.查看视频是否退出全屏"):
                pass
            sleep(5)
        except:
            pass
        finally:
            self.wsy.pagejiamingceshi_return_to_wsypage_keycode()